﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using EnvDTE;
using Microsoft.Win32;

namespace VsTortoiseSVN.OptionPages
{
    /// <summary>
    /// Represents a page inside the Visual Studio Tools Options dialog.
    /// Unlike in a VsPackage project, for add-ins the page has to be
    /// derived from UserControl rather than DialogPage:
    /// http://social.msdn.microsoft.com/Forums/en/vsx/thread/4a9ae777-1e91-43ed-b69c-6bdd4c2e4151
    /// 
    /// Derive OptionPages from this class!
    /// </summary>
    public class PageBase : UserControl, IDTToolsOptionsPage
    {
        /// <summary>
        /// Constructor
        /// </summary>
        protected PageBase()
        {
        }

        /// <summary>
        /// Initialize the Page.
        /// This sets the font of all controls to a system default font,
        /// loads texts from the resource file and sets those to each Control where specified.
        /// If a Control on the page has as text a resource string set, it will replace the
        /// resource string identifier by the actual text.
        /// </summary>
        protected void InitializePage()
        {
            SetResourceTextRecursive(this);
            SetFontRecursive(this, SystemFonts.MessageBoxFont);
        }

        /// <summary>
        /// Sets the specified font to the control and all its sub-controls
        /// </summary>
        private void SetFontRecursive(Control control, Font font)
        {
            control.Font = font;

            foreach (Control ctrl in control.Controls)
            {
                SetFontRecursive(ctrl, font);
            }
        }

        /// <summary>
        /// Sets resource texts to each control where possible
        /// </summary>
        private void SetResourceTextRecursive(Control control)
        {
            // Has the control text a resource string counterpart?
            if (control.Tag != null && control.Tag is string && SatelliteDLL.Instance.HasText(control.Tag as string))
                control.Text = SatelliteDLL.Instance.GetText(control.Tag as string);

            foreach (Control ctrl in control.Controls)
            {
                SetResourceTextRecursive(ctrl);
            }
        }

        #region IDTToolsOptionsPage Implementation
        public virtual void GetProperties(ref object PropertiesObject)
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.GetProperties");
        }

        public virtual void OnAfterCreated(DTE DTEObject)
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnAfterCreated");
        }

        public virtual void OnCancel()
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnCancel");
        }

        public virtual void OnHelp()
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnHelp");
        }

        public virtual void OnOK()
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnOK");
        }
        #endregion
    }
}
